package com.aaa.controller;

import com.aaa.entity.Permission;
import com.aaa.entity.Role;
import com.aaa.service.AlbumService;
import com.aaa.service.EmpService;
import com.aaa.service.PermissionService;
import com.aaa.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.List;

@RequestMapping("role")
@Controller
public class RoleController {
    @Resource
    RoleService roleService;
    @Resource
    AlbumService albumService;
    @Resource
    EmpService empService;
    @Resource
    PermissionService permissionService;
//用于给下拉框赋值
    @RequestMapping("selectRole")
    @ResponseBody
    public List<Role> selectRole(){
        List<Role> list = roleService.list();
        return list;
    }


    @RequestMapping("selectRole1")
    @ResponseBody
    public PageInfo<Role> select(Integer pageNum, Integer pageSize, String rolename) {
        if(rolename!=null){
            //模糊查询
            PageHelper.startPage(pageNum,pageSize);
            QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("rolename",rolename);
            List<Role> list=roleService.list(queryWrapper);
            PageInfo<Role> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }else {
            //查询全部
            PageHelper.startPage(pageNum,pageSize);
            List<Role> list=roleService.list();
            PageInfo<Role> pageInfo=new PageInfo<>(list);
            return  pageInfo;
        }

    }
    //批量删除
    @RequestMapping("delRole")
    @ResponseBody
    public boolean delRole(String str) {
        String[] rid= str.split(",");
        boolean info=true;
        for (int i=0;i<rid.length;i++) {
            QueryWrapper<com.aaa.entity.Emp> queryWrapper=new QueryWrapper<com.aaa.entity.Emp>();
            queryWrapper.eq("roleid",rid[i]);
            List<com.aaa.entity.Emp> list = empService.list(queryWrapper);
            //删除角色前先查询该角色下有没有员工
            if(list.size()!=0){
                info=false;
            }else {//如果选中的角色其中有一个不能删除其余的也不能删除
                if(info==true){
                    boolean b = roleService.removeById(rid[i]);
                }
            }
        }
        return info;
    }
    //角色信息修改
    @RequestMapping("updRole")
    @ResponseBody
    public boolean updRole(Role role) {
        boolean b = roleService.updateById(role);
        return true;
    }

    //角色添加
    @RequestMapping("roleAdd")
    @ResponseBody
    public boolean roleAdd(Role role) {
        boolean result=roleService.save(role);
        return result;
    }
    //setPermission
    //设置权限
    @RequestMapping("setPermission")
    @ResponseBody
    public boolean setPermission(Integer rid,String str) {
        //设置权限前先删除角色拥有的权限
        UpdateWrapper<Permission> updateWrapper=new UpdateWrapper<Permission>();
         updateWrapper.eq("rid", rid);
        boolean remove = permissionService.remove(updateWrapper);
            boolean save = false;

            String[] pname = str.split(",");
            Permission permission = new Permission();
            for (int i = 0; i < pname.length; i++) {
                permission.setRid(rid);
                permission.setPname(pname[i]);
                save = permissionService.save(permission);
            }

        // boolean result=roleService.save(role);
        return save;
    }
    //setPermission
    //得到角色拥有的权限
    @RequestMapping("getPermission")
    @ResponseBody
    public String[] getPermission(Integer rid) {
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rid", rid);
     List<Permission> list=permissionService.list(queryWrapper);
        String[] pnam = new String[list.size()];
     for (int i=0;i<list.size();i++){
         pnam[i]=list.get(i).getPname();
     }
      return pnam;
    }


}
